﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.ServiceHosting.StorageClient;
using Microsoft.ServiceHosting.ServiceRuntime;

namespace HollywoodHackers.Storage.Table
{
    public class UserNotificationRepository : RepositoryBase
    {
        private UserTextNotificationContext context;

        public UserNotificationRepository(StorageAccountInfo blob, StorageAccountInfo queue, StorageAccountInfo table)
            : base(blob, queue, table)
        {
            context = new UserTextNotificationContext();
            InitTableStorage(RoleManager.GetConfigurationSetting("UserNotificationsTableName"));
        }

        public UserTextNotification[] GetNotifications(string userName)
        {
            TableStorageDataServiceContext svc = tableStorage.GetDataServiceContext();

            IEnumerable<UserTextNotification> results = from UserTextNotification toast in svc.CreateQuery<UserTextNotification>("UserNotifications")
                                                        where toast.PartitionKey == userName
                                                        select toast;
            return results.ToArray();
        }

        public void AddNotification(UserTextNotification notification, string userName)
        {
            TableStorageDataServiceContext svc = tableStorage.GetDataServiceContext();

            notification.RowKey = Guid.NewGuid().ToString();
            notification.PartitionKey = userName;
            svc.AddObject(RoleManager.GetConfigurationSetting("UserNotificationsTableName"), notification);
            svc.SaveChanges();
        }
    }
}
